package com.pubpage.flow.flow_note_list;

import java.util.*;

import com.pub.database.*;

public class Business {
   QueryBean gqbnDAO=null;

   public Business(QueryBean pqbnDAO){
      gqbnDAO=pqbnDAO;
   }

   /**
    * getData
    *
    * @param view View
    * @param pstrUserNO String
    * @return String
    */
   String getData(View view, String pstrUserNO) throws Exception{

       return "";
    }

    /**
     * setButtonShow
     *
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserID String
     */
    void setButtonShow(View view,String pstrPrgAction, String pstrPrgStatus,
                       String pstrUserID) throws Exception{
    }

    void clearData(View view) throws Exception{
    }

    /**
     * setReadonlyFields
     *
     * @param view View
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserNO String
     */
    void setReadonlyFields(View view, String pstrPrgAction,
                           String pstrPrgStatus, String pstrUserNO) {
    }

    /**
     * setShowFields
     *
     * @param view View
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserNO String
     */
    void setShowFields(View view, String pstrPrgAction, String pstrPrgStatus,
                       String pstrUserNO) {
    }

    /**
     * initAction
     *
     * @param view View
     * @param pstrUserNO String
     */
    String initAction(View view, String pstrUserNO) throws Exception{
        Hashtable<String,Hashtable<String,Object>> lhtNoteData=null;

        try{
            if (view.getNote_kind() != null && view.getNote_kind().equals("L")) { //approve
                view.setPrgTitle("審核意見");
                view.setWriter_title("審核者:");
                view.setWrite_no_title("審核站別:");
                view.setWrite_time_title("審核時間:");
                view.setWrite_note_title("審核意見:");

                view.setTd_header_css("td_head_1");
                view.setTd_content_css("td_content_1");
            } else if (view.getNote_kind() != null && view.getNote_kind().equals("N")) { //reject
                view.setPrgTitle("駁回內容");
                view.setWriter_title("駁回者:");
                view.setWrite_no_title("駁回站別:");
                view.setWrite_time_title("駁回時間:");
                view.setWrite_note_title("駁回原因:");

                view.setTd_header_css("td_head_2");
                view.setTd_content_css("td_content_2");
            }

            lhtNoteData = queryNoteData(view.getFlow_num(), view.getNote_kind());

            if (lhtNoteData == null) {
                return "login"; //若沒有資料則return "login" 這樣才不會輸出任何html
            }

            view.setNote_list_html(getNoteList(view,lhtNoteData));

            view.setOnloadScript("set_window_on_middle(window.window);");
        }
        catch(Exception e){
            throw new Exception("讀取資料錯誤(flow_note_list.business.init()==>" + e.toString());
        }
        finally{
            if(lhtNoteData!=null){
                lhtNoteData.clear();
                lhtNoteData=null;
            }
        }
        return "";
    }

    /**
     * getNoteList
     *
     * @param phtNoteData Hashtable
     * @return String
     */
    private String getNoteList(View view,Hashtable<String,Hashtable<String,Object>> phtNoteData) throws Exception{
        StringBuffer lstbNoteListHtml=new StringBuffer();

        lstbNoteListHtml.append("<TR>\r\n");
        lstbNoteListHtml.append("<TD colspan='6' CLASS='");
        lstbNoteListHtml.append(view.getTd_header_css());
        lstbNoteListHtml.append("'><div align='center'>");
        lstbNoteListHtml.append(view.getPrgTitle());
        lstbNoteListHtml.append("</div></TD>\r\n");
        lstbNoteListHtml.append("</TR>\r\n");

        for(int i=0;i<phtNoteData.size();i++){
            lstbNoteListHtml.append("<TR>\r\n");
            lstbNoteListHtml.append("<TD width='15%' CLASS='");
            lstbNoteListHtml.append(view.getTd_header_css());
            lstbNoteListHtml.append("' nowrap>");
            lstbNoteListHtml.append(view.getWriter_title());
            lstbNoteListHtml.append("</TD>\r\n");
            lstbNoteListHtml.append("<TD width='18%' CLASS='");
            lstbNoteListHtml.append(view.getTd_content_css());
            lstbNoteListHtml.append("'>");
            lstbNoteListHtml.append(((Hashtable<String,Object>)phtNoteData.get(String.valueOf(i))).get("EMPLOYEE_NO"));
            lstbNoteListHtml.append(((Hashtable<String,Object>)phtNoteData.get(String.valueOf(i))).get("EMPLOYEE_NAME_C"));
            lstbNoteListHtml.append("</td>\r\n");
            lstbNoteListHtml.append("<TD CLASS='");
            lstbNoteListHtml.append(view.getTd_header_css());
            lstbNoteListHtml.append("' nowrap >");
            lstbNoteListHtml.append(view.getWrite_no_title());
            lstbNoteListHtml.append("</td>\r\n");
            lstbNoteListHtml.append("<TD CLASS='");
            lstbNoteListHtml.append(view.getTd_content_css());
            lstbNoteListHtml.append("'>");
            lstbNoteListHtml.append(((Hashtable<String,Object>)phtNoteData.get(String.valueOf(i))).get("FLOW_NO"));
            lstbNoteListHtml.append(((Hashtable<String,Object>)phtNoteData.get(String.valueOf(i))).get("FLOW_DESC"));
            lstbNoteListHtml.append("</td>\r\n");
            lstbNoteListHtml.append("<TD width='15%' CLASS='");
            lstbNoteListHtml.append(view.getTd_header_css());
            lstbNoteListHtml.append("'>");
            lstbNoteListHtml.append(view.getWrite_time_title());
            lstbNoteListHtml.append("</TD>\r\n");
            lstbNoteListHtml.append("<TD width='20%' CLASS='");
            lstbNoteListHtml.append(view.getTd_content_css());
            lstbNoteListHtml.append("'>");
            lstbNoteListHtml.append(((Hashtable<String,Object>)phtNoteData.get(String.valueOf(i))).get("SIGN_DATE"));
            lstbNoteListHtml.append("</td>\r\n</tr>\r\n");
            lstbNoteListHtml.append("<TR>\r\n");
            lstbNoteListHtml.append("<TD CLASS='");
            lstbNoteListHtml.append(view.getTd_header_css());
            lstbNoteListHtml.append("' width='15%' nowrap >");
            lstbNoteListHtml.append(view.getWrite_note_title());
            lstbNoteListHtml.append("</td>\r\n");
            lstbNoteListHtml.append("<TD CLASS='");
            lstbNoteListHtml.append(view.getTd_content_css());
            lstbNoteListHtml.append("' colspan='5'>");
            lstbNoteListHtml.append(((Hashtable<String,Object>)phtNoteData.get(String.valueOf(i))).get("SIGN_NOTE"));
            lstbNoteListHtml.append("</td>\r\n");
            lstbNoteListHtml.append("</TR>\r\n");
        }

        return lstbNoteListHtml.toString();
    }

    private Hashtable<String,Hashtable<String,Object>> queryNoteData(String pstrFlowNum,String pstrNoteKind) throws Exception{
        StringBuffer lstbSQL=new StringBuffer();

        lstbSQL.append("select a.flow_no,c.flow_desc,a.employee_no,b.employee_name_c,a.sign_note,to_char(a.sign_date,'yyyy/mm/dd hh24:mi:ss') as sign_date ");
        lstbSQL.append("from wkf_log a, pnl_employee b,floweng_setup c ");
        lstbSQL.append("where a.flow_code='");
        lstbSQL.append(pstrFlowNum.substring(0,3));
        lstbSQL.append("' and a.flow_num='");
        lstbSQL.append(pstrFlowNum);
        lstbSQL.append("' and a.employee_no=b.employee_no ");
        lstbSQL.append("and a.flow_code=c.flow_code and a.flow_no=c.flow_no and a.log_st='");
        lstbSQL.append(pstrNoteKind);
        lstbSQL.append("' order by a.sign_date desc");

        return gqbnDAO.hashtableSQL(lstbSQL.toString());
    }
}
